from re import I
import sqlite3

from dto.stock_risk_assessment_item import AntRiskAssessmentItemDto
from .database_pool import DatabasePool
from peewee import *
from dto.stock_risk_assessment import RiskAssessmentDTO
from datetime import datetime


class AntRiskAssessmentItem(Model):
    # ID 主键，自动递增
    id = IntegerField(primary_key=True)

    # 股票代码
    stock_code = CharField(max_length=50, null=False)

    # # 上传更新时间
    # create_time = TimestampField(default="CURRENT_TIMESTAMP")

    # # 风险描述
    # description = TextField(null=True)

    # # 风险类型 (例如：市场风险、流动性风险)
    # risk_type = CharField(max_length=50, null=True)

    # # 风险等级 (0 - 10级)
    # risk_level = IntegerField(
    #     constraints=[Check("risk_level BETWEEN 0 AND 10")], null=True
    # )

    # # 匹配标签
    # stock_tag_code = CharField(max_length=50, null=True)

    # # 策略 Code
    # strategy_code = CharField(max_length=50, null=False)

    # # 策略可读名称
    # strategy_name = CharField(max_length=100, null=False)

    # # 策略分组 (0: 风险型, 1: 关注型)
    # strategy_group = IntegerField(
    #     constraints=[Check("strategy_group IN (0, 1)")], null=True
    # )

    # # 备用字段：策略类型
    # strategy_type = CharField(max_length=50, null=True)

    # # 策略分析的天数
    # analysis_day = IntegerField(null=True)

    # # 策略等级 (等级越高, 分数比例越高)
    # strategy_level = IntegerField(null=True)

    class Meta:
        database = DatabasePool(max_connections=5).database  # 指定数据库连接
        table_name = "ant_risk_assessment_item"


class RiskAssessmentItemService:
    # 新增风险评估记录
    def add_risk_assessment_item(self, item_dto: AntRiskAssessmentItemDto):
        """
        新增风险评估记录
        :param item_dto: AntRiskAssessmentItemDto 对象
        """
        try:
            # AntRiskAssessmentItem.create(
            # stock_code=item_dto.stock_code,
            # description=item_dto.description,
            # risk_type=item_dto.risk_type,
            # risk_level=int(item_dto.risk_level),  # 确保是整数
            # stock_tag_code=item_dto.stock_tag_code,
            # strategy_code=item_dto.strategy_code,
            # strategy_name=item_dto.strategy_name,
            # strategy_group=item_dto.strategy_group,
            # strategy_type=item_dto.strategy_type,
            # analysis_day=int(item_dto.analysis_day),  # 确保是整数
            # strategy_level=int(item_dto.strategy_level),  # 确保是整数
            # )
            AntRiskAssessmentItem.create(
                stock_code="000001",  # 示例股票代码
                description="示例风险描述",  # 风险描述
                risk_type="市场风险",  # 风险类型
                risk_level=5,  # 风险等级
                stock_tag_code="TAG001",  # 匹配标签
                strategy_code="STR001",  # 策略 Code
                strategy_name="成交量分析策略",  # 策略可读名称
                strategy_group=0,  # 策略分组 (0: 风险型)
                strategy_type="Volume Analysis",  # 策略类型
                analysis_day=3,  # 策略分析的天数
                strategy_level=2,  # 策略等级
            )

            print("风险评估记录已添加。")
        except ValueError as ve:
            print(f"字段类型转换错误: {ve}")
        except Exception as e:
            print(f"ant_risk_assessment_item 新增记录时出错: {e}")

    # 根据股票代码更新风险评估记录
    def update_risk_assessment_item_by_code(
        self, stock_code: str, updated_dto: AntRiskAssessmentItemDto
    ):
        """
        根据股票代码更新风险评估记录
        :param stock_code: 股票代码
        :param updated_dto: 包含要更新字段的 AntRiskAssessmentItemDto 对象
        """
        try:
            item = AntRiskAssessmentItem.get(
                AntRiskAssessmentItem.stock_code == stock_code
            )

            # 更新字段
            for field, value in updated_dto.__dict__.items():
                if hasattr(item, field) and value is not None:
                    setattr(item, field, value)

            # 更新修改时间（如有需求）
            item.modified_date = datetime.now()

            item.save()
            print(f"股票代码 {stock_code} 的记录已更新。")
        except DoesNotExist:
            print(f"未找到股票代码为 {stock_code} 的记录。")
        except Exception as e:
            print(f"更新记录时出错: {e}")

    # 获取所有风险评估记录
    def get_all_risk_assessment_items(self) -> list[AntRiskAssessmentItemDto]:
        """
        获取所有风险评估记录
        :return: AntRiskAssessmentItemDto 对象列表
        """
        try:
            items = AntRiskAssessmentItem.select()
            return [
                AntRiskAssessmentItemDto(
                    stock_code=item.stock_code,
                    create_time=item.create_time,
                    description=item.description,
                    risk_type=item.risk_type,
                    risk_level=item.risk_level,
                    stock_tag_code=item.stock_tag_code,
                    strategy_code=item.strategy_code,
                    strategy_name=item.strategy_name,
                    strategy_group=item.strategy_group,
                    strategy_type=item.strategy_type,
                    analysis_day=item.analysis_day,
                    strategy_level=item.strategy_level,
                )
                for item in items
            ]
        except Exception as e:
            print(f"获取记录时出错: {e}")
            return []

    # 根据股票代码删除风险评估记录
    def delete_risk_assessment_item_by_code(self, stock_code: str):
        """
        根据股票代码删除风险评估记录
        :param stock_code: 股票代码
        """
        try:
            item = AntRiskAssessmentItem.get(
                AntRiskAssessmentItem.stock_code == stock_code
            )
            item.delete_instance()
            print(f"股票代码 {stock_code} 的记录已删除。")
        except DoesNotExist:
            print(f"未找到股票代码为 {stock_code} 的记录。")
        except Exception as e:
            print(f"删除记录时出错: {e}")

    # 根据股票代码查询单条风险评估记录
    def get_risk_assessment_item_by_code(
        self, stock_code: str
    ) -> AntRiskAssessmentItemDto:
        """
        根据股票代码查询单条风险评估记录
        :param stock_code: 股票代码
        :return: 查询到的 AntRiskAssessmentItemDto 对象或 None
        """
        try:
            item = AntRiskAssessmentItem.get(
                AntRiskAssessmentItem.stock_code == stock_code
            )
            return AntRiskAssessmentItemDto(
                stock_code=item.stock_code,
                create_time=item.create_time,
                description=item.description,
                risk_type=item.risk_type,
                risk_level=item.risk_level,
                stock_tag_code=item.stock_tag_code,
                strategy_code=item.strategy_code,
                strategy_name=item.strategy_name,
                strategy_group=item.strategy_group,
                strategy_type=item.strategy_type,
                analysis_day=item.analysis_day,
                strategy_level=item.strategy_level,
            )
        except DoesNotExist:
            print(f"未找到股票代码为 {stock_code} 的记录。")
            return None
        except Exception as e:
            print(f"查询记录时出错: {e}")
            return None
